package cn.geminis.lowcode.compiler.io;

import com.sun.source.tree.ClassTree;

import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.tools.JavaFileObject;
import java.io.*;
import java.net.URI;

/**
 * @author puddi
 */
public class TreeFileObject implements JavaFileObject {

    private final ClassTree tree;

    public TreeFileObject(ClassTree tree) {
        this.tree = tree;
    }

    @Override
    public Kind getKind() {
        return Kind.SOURCE;
    }

    @Override
    public boolean isNameCompatible(String simpleName, Kind kind) {
        return this.tree.getSimpleName().toString().equals(simpleName);
    }

    @Override
    public NestingKind getNestingKind() {
        return NestingKind.TOP_LEVEL;
    }

    @Override
    public Modifier getAccessLevel() {
        return Modifier.PUBLIC;
    }

    @Override
    public URI toUri() {
        return null;
    }

    @Override
    public String getName() {
        return tree.getSimpleName().toString();
    }

    @Override
    public InputStream openInputStream() {
        return new ByteArrayInputStream(this.tree.toString().getBytes());
    }

    @Override
    public OutputStream openOutputStream() throws IOException {
        throw new UnsupportedEncodingException();
    }

    @Override
    public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
        throw new UnsupportedEncodingException();
    }

    @Override
    public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
        throw new UnsupportedEncodingException();
    }

    @Override
    public Writer openWriter() throws IOException {
        throw new UnsupportedEncodingException();
    }

    @Override
    public long getLastModified() {
        return 0;
    }

    @Override
    public boolean delete() {
        return false;
    }
}
